package com.src;

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.content.res.TypedArray;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class GrallyNewUIActivity extends Activity {

	private Gallery gallery = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Reference the Gallery view
		gallery = (Gallery) findViewById(R.id.Gallery);
		// Set the adapter to our custom adapter (below)
		gallery.setAdapter(new ImageAdapter(this));

		// // Set a item click listener, and just Toast the clicked position
		// g.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView parent, View v, int position,
		// long id) {
		// Toast.makeText(GrallyNewUIActivity.this, "" + position,
		// Toast.LENGTH_SHORT).show();
		// }
		// });

		// We also want to show context menu for longpressed items in the
		// gallery
		// registerForContextMenu(g);
	}

	// @Override
	// public void onCreateContextMenu(ContextMenu menu, View v,
	// ContextMenuInfo menuInfo) {
	// menu.add(R.string.gallery_2_text);
	// }

	// @Override
	// public boolean onContextItemSelected(MenuItem item) {
	// AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
	// .getMenuInfo();
	// Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
	// .show();
	// return true;
	// }

	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context context = null;
		private LayoutInflater inflater = null;

		public ImageAdapter(Context context) {
			// mContext = c;
			// // See res/values/attrs.xml for the <declare-styleable> that
			// defines
			// // Gallery1.
			// TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			// mGalleryItemBackground = a.getResourceId(
			// R.styleable.Gallery1_android_galleryItemBackground, 0);
			// a.recycle();
			super();
			this.context = context;
		}

		public int getCount() {
			return Integer.MAX_VALUE;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (null != context) {
				inflater = LayoutInflater.from(context);
			}

			if (null == convertView) {
				// convertView=LayoutInflater.from(context).inflate(R.layout.grallyitems,null);
				convertView = inflater.inflate(R.layout.grallyitems, null);
			}

			ImageView image = (ImageView) convertView.findViewById(R.id.image);
			// ImageView i = new ImageView(mContext);
			TextView imageText = (TextView) convertView
					.findViewById(R.id.ImageText);
			RatingBar ratingBar = (RatingBar) convertView
					.findViewById(R.id.ImageRatingBar);
			image.setImageResource(mImageIds[position % mImageIds.length]);
			imageText.setText("免费");
			ratingBar.setIsIndicator(true);
			// image.setScaleType(ImageView.ScaleType.FIT_XY);
			// image.setLayoutParams(new Gallery.LayoutParams(136, 88));

			// The preferred Gallery item background
			// image.setBackgroundResource(mGalleryItemBackground);

			return convertView;
		}

		private Integer[] mImageIds = { R.drawable.p_a, R.drawable.p_b,
				R.drawable.p_c, R.drawable.p_d, R.drawable.p_e, R.drawable.p_f,
				R.drawable.p_g, R.drawable.p_h, };
	}

}
